package crivia.ecp.carry.em;

import crivia.ecp.carry.html.X;
import crivia.ecp.carry.html.form.Input;
import crivia.ecp.common.ECR;
import crivia.ecp.i.DI;
import crivia.time.carry.Time;
import crivia.time.exception.WrongTimeFormat;

public class TimeInput implements DI {
	
	public TimeInput(){}
	
	public TimeInput(String key , String value){
		this.key = key;
		setTime(value);
	}
	
	public TimeInput(String key , int hour, int minute, int second){
		this.key = key;
		this.hour = hour;
		this.minute = minute;
		this.second = second;
	}
	
	public TimeInput(String key , String value 
			, String hourRequirdString, String minuteRequirdString, String secondRequirdString){
		this.key = key;
		setTime(value);
		this.hourRequirdString = hourRequirdString;
		this.minuteRequirdString = minuteRequirdString;
		this.secondRequirdString = secondRequirdString;
	}
	
	private TimeInput setTime(String s){
		try {
			Time time = Time.toTime(s);
			this.hour = time.getHour();
			this.minute = time.getMinute();
			this.second = time.getSecond();
		} 
		catch (WrongTimeFormat e) {}
		catch (NullPointerException e) {}
		return this;
	}
	
	private String key;
	public String getKey() {return key;}
	public TimeInput setKey(String key) {this.key = key;return this;}

	protected String scriptKey;
	public String getScriptKey() {
		if (!ECR.hasCondition(scriptKey)){
			scriptKey = key;
		}
		return scriptKey;
	}
	public TimeInput setScriptKey(String scriptKey) {
		this.scriptKey = scriptKey;
		return this;
	}
	
	private int hour;
	public int getHour() {return hour;}
	public TimeInput setHour(int hour) {this.hour = hour;return this;}
	private String hourRequirdString;
	public String getHourRequirdString() {return hourRequirdString;}
	public TimeInput setHourRequirdString(String s) {this.hourRequirdString = s;return this;}
	private int minute;
	public int getMinute() {return minute;}
	public TimeInput setMinute(int minute) {this.minute = minute;return this;}
	private String minuteRequirdString;
	public String getMinuteRequirdString() {return minuteRequirdString;}
	public TimeInput setMinuteRequirdString(String s) {this.minuteRequirdString = s;return this;}
	private int second;
	public int getSecond() {return second;}
	public TimeInput setSecond(int second) {this.second = second;return this;}
	private String secondRequirdString;
	public String getSecondRequirdString() {return secondRequirdString;}
	public TimeInput setSecondRequirdString(String s) {this.secondRequirdString = s;return this;}
	@Override
	public String getText() {return ""+new Time(hour, minute, second, null);}
	@Override
	public String getValue() {return getText();}

	@Override
	public DI setValue(String s) {
		Time time = Time.toTime(s);
		hour = time.getHour();
		minute = time.getMinute();
		second = time.getSecond();
		return this;
	}
	
	@Override
	public String toString() {
		String jsKey = ECR.ns(getScriptKey())
				.replaceAll("[.]", "_")
				.replaceAll("-", "_");
		String jsMethod = jsKey+"_TimeFormat";
		String jsMethod2 = jsKey+"_Focus";
		String jsMethod3 = jsKey+"_Focus(this)";
		return ""
			+new Input(key+".hour"
				, hour+"", hourRequirdString)
			.setScriptKey(jsKey+".hour")
			.addStyle("width", "26px")
			.addAttr("onfocus", jsMethod3)
			+":"
			+new Input(key+".minute"
				, minute+"", minuteRequirdString)
			.setScriptKey(jsKey+".minute")
			.addStyle("width", "26px")
			.addAttr("onfocus", jsMethod3)
			+":"
			+new Input(key+".second"
				, second+"", secondRequirdString)
			.setScriptKey(jsKey+".second")
			.addStyle("width", "26px")
			.addAttr("onfocus", jsMethod3)
			+X.JavaSctipt(""
				+"\nwindow."+jsKey+"_hourEC = function(p){"
				+"\n	"+jsMethod+"(p);"
				+"\n};"
				+"\nwindow."+jsKey+"_minuteEC = function(p){"
				+"\n	"+jsMethod+"(p);"
				+"\n};"
				+"\nwindow."+jsKey+"_secondEC = function(p){"
				+"\n	"+jsMethod+"(p);"
				+"\n};"
				+"\nwindow."+jsMethod2+" = function(o){"
				+"\n	o.value = 0;"
				+"\n	jQuery(o).select();"
				+"\n};"
				+"\nwindow."+jsMethod+" = function(p){"
				+"\n	if (p.v.o == undefined && p.v.n){"
				+"\n		return;"
				+"\n	}"
				+"\n	var o = document.getElementById(p.k);"
				+"\n	var id = o.id;"
				+"\n	if (isNaN(o.value)){"
				+"\n		o.value = '';"
				+"\n	}"
				+"\n	var hk = '"+jsKey+".hour';"
				+"\n	var mk = '"+jsKey+".minute';"
				+"\n	var sk = '"+jsKey+".second';"
				+"\n	if (o.value.length < 2"
				+"\n	 && (id != hk"
				+"\n	 || o.value == '0'"
				+"\n	 || o.value == '1'"
				+"\n	 || o.value == '2')"
				+"\n	 && parseInt(o.value) < 6"
				+"\n	){"
				+"\n		return;"
				+"\n	}"
				+"\n	var bz = function(t){"
				+"\n		while (t != '0' && t.indexOf('0') == 0){"
				+"\n			t = t.substring(1);"
				+"\n		}"
				+"\n		return t;"
				+"\n	};"
				+"\n	if (id == hk){"
				+"\n		var h = parseInt(bz(_E.v(hk)));"
				+"\n		if (isNaN(h) || h < 0){"
				+"\n			h = 0;"
				+"\n		} else if (h > 23){"	
				+"\n			h = 23;"
				+"\n		}"
				+"\n		_E.v(hk , h);"
				+"\n		document.getElementById(mk).focus();"
				+"\n	} else if (id == mk){"
				+"\n		var m = parseInt(bz(_E.v(mk)));"
				+"\n		if (isNaN(m) || m < 0){"
				+"\n			m = 0;"
				+"\n		} else if (m > 59){"	
				+"\n			m = 59;"
				+"\n		}"
				+"\n		_E.v(mk , m);"
				+"\n		document.getElementById(sk).focus();"
				+"\n	} else if (id == sk){"
				+"\n		var s = parseInt(bz(_E.v(sk)));"
				+"\n		if (isNaN(s) || s < 0){"
				+"\n			s = 0;"
				+"\n		} else if (s > 59){"	
				+"\n			s = 59;"
				+"\n		}"
				+"\n		_E.v(sk , s);"
				+"\n	}"
				+"\n};"
			);
	}

}
